package com.raising.modules.sys.controller;

import com.raising.StaticParam;
import com.raising.framework.controller.BaseController;
import com.raising.framework.entity.ResultVo;
import com.raising.framework.interceptor.Page;
import com.raising.modules.sys.entity.MsgReceive;
import com.raising.modules.sys.entity.MsgSend;
import com.raising.modules.sys.service.MsgReceiveService;
import com.raising.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* 消息接收表 控制器
* @author gaoy
* @version 1.0
*/

@Controller
@RequestMapping("${adminPath}/sys/msgReceive")
public class MsgReceiveController extends BaseController {

    @Autowired
    private MsgReceiveService msgReceiveService;

    /**
     * 获取消息列表
     * @param model
     * @param page
     * @return
     */
    /**
     * 跳转至列表页面
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     */
    @RequiresPermissions("sys:msgReceive:view")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list() {
        olog("进入消息管理消息分页页面");
        return "sys/msgReceive/list";
    }

    /**
     * 加载列表分页数据
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     */
    @RequiresPermissions("sys:msgReceive:view")
    @RequestMapping(value = "/getPage.json", method = RequestMethod.POST)
    public @ResponseBody
    ResultVo getPage(MsgReceive msgReceive, MsgSend msgSend, Model model, Page<MsgReceive> page)throws Exception {
        olog("进入消息管理消息分页页面");
        msgSend.setType("notice");
        msgReceive.setReceiveUserId(UserUtils.getUserId());
        msgReceive.setMsgSend(msgSend);
        msgReceive.setStatus(StaticParam.VALID_STRING);
        page.setEntity(msgReceive);
        page.setOrderBy(" a.create_date desc");
        return ResultVo.success().data(msgReceiveService.getPage(page));
    }


    /**
     * 删除 消息通知
     * @author luxy
     * @createTime 2018-01-11 20:28:30
     * @param id
     */
    @RequestMapping(value = "/delete.json", method = RequestMethod.POST)
    public @ResponseBody ResultVo deleteJson(String id) throws Exception{
        return msgReceiveService.delete(id);
    }

    /**
     * 查看消息详细
     * @param id
      * @param model
     * @return
     */
    @RequestMapping(value = "/view", method = RequestMethod.GET)
    public String update(String id,Model model) throws Exception {
        MsgReceive msgReceive = msgReceiveService.get(id);
        if(!StaticParam.YES_STRING.equals(msgReceive.getIsRead())){
            MsgReceive param = new MsgReceive();
            param.setId(id);
            param.setIsRead(StaticParam.YES_STRING);
            param.setUpdateBy(UserUtils.getUserId());
            msgReceiveService.updateToRead(param);
        }
        model.addAttribute("msgReceive",msgReceive);
        return "sys/msgReceive/view";
   }

    /**
     * 查询当前所有未读的消息
     * @return
     */
    @RequestMapping(value = "/queryAllNoticeMsg.json", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo queryAllNoticeMsg() throws Exception{
        //查询当前登录人员的所有信息
        MsgReceive msgReceive = new MsgReceive();
        msgReceive.setReceiveUserId(UserUtils.getUserId());
        msgReceive.setStatus(StaticParam.VALID_STRING);
        //只查询未读的信息
        msgReceive.setIsRead(StaticParam.NO_STRING);

        MsgSend msgSend = new  MsgSend();
        msgSend.setType("notice");
        msgReceive.setMsgSend(msgSend);
        //根据时间倒序排序
        msgReceive.setOrderBy(" a.create_date desc");
        return ResultVo.success().data(msgReceiveService.getList(msgReceive));
    }
    /**
     * 主页查询消息详细
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "/homeView", method = RequestMethod.GET)
    public String updateHome(String id,Model model) throws Exception{
        MsgReceive msgReceive = msgReceiveService.get(id);
        if(!StaticParam.YES_STRING.equals(msgReceive.getIsRead())){
            MsgReceive param = new MsgReceive();
            param.setId(id);
            param.setIsRead(StaticParam.YES_STRING);
            param.setUpdateBy(UserUtils.getUserId());
            msgReceiveService.updateToRead(param);
        }
        model.addAttribute("msgReceive", msgReceive);
        return "sys/msgReceive/homeView";
    }
}
